//
//  UIView+Gradual.swift
//  MedicalUnion
//
//  Created by William_Xue on 2018/1/3.
//  Copyright © 2018年 William_Xue. All rights reserved.
//

import Foundation
import UIKit

extension UIView{
    
    /// 为View添加简单的渐变色背景
    ///
    /// - Parameters:
    ///   - direction: 渐变方向
    ///   - startColor: 开始颜色
    ///   - endColor: 结尾颜色
    ///   - frame: 背景位置及大小
    func gradualBackgroundWithDirection(_ direction:GradualDirection, startColor:UIColor, endColor:UIColor, frame:CGRect) {
        let gradientLayer = CAGradientLayer.init()
        gradientLayer.colors = [startColor.cgColor,endColor.cgColor];
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        if direction == .horizontal {
            gradientLayer.endPoint = CGPoint(x: 1.0, y: 0)
        }else{
            gradientLayer.endPoint = CGPoint(x: 0, y: 1.0)
        }
        gradientLayer.frame = frame
        self.layer.insertSublayer(gradientLayer, at: 0)
    }
    
    /// 渐变颜色的方向
    ///
    /// - horizontal: 水平
    /// - vertical: 垂直
    enum GradualDirection {
        case horizontal
        case vertical
    }
}
